package com.mobilebsc.datamanager;

import java.util.ArrayList;
import java.util.List;

import com.mobilebsc.datatype.StrategicGoal;
import com.mobilebsc.exception.NotImplementedException;
import com.mobilebsc.httpclient.callback.IAsyncTaskCompleteListener;
import com.mobilebsc.httpclient.callback.StrategicGoalRestClientUsage;

public class StrategicGoalsDataManager implements IDataManager<StrategicGoal>,
		IAsyncTaskCompleteListener<List<StrategicGoal>> {
 
	private StrategicGoalRestClientUsage restClientUsage;
	private List<StrategicGoal> resultList;
	private List<StrategicGoal> singleResultList;
	private IDataReadyListener dataReadyListener;
	
	public StrategicGoalsDataManager(IDataReadyListener dataReadyListener) {
		restClientUsage = new StrategicGoalRestClientUsage(this);
		resultList = null;
		singleResultList = null;
		this.dataReadyListener = dataReadyListener;
	}
	
	@Override
	public void getAll() {
		restClientUsage.getAll();
	}

	@Override
	public void getById(int id) {
		restClientUsage.getById(id);
	}
	
	public void getByPerspectiveId(int perspectiveId) {
		restClientUsage.getByPerspectiveId(perspectiveId);
	}
	
	public void getReferencedStrategicGoals(int strategicGoalId) {
		restClientUsage.getReferencedStrategicGoals(strategicGoalId);
	}

	@Override
	public List<StrategicGoal> loadAll() {
		if (resultList != null) {
			return resultList;
		} else {
			restClientUsage.getAll();
			return new ArrayList<StrategicGoal>();
		}
	}

	@Override
	public List<StrategicGoal> loadById(int id) {
		if (resultList != null) {
			for(StrategicGoal sg : resultList) {
				if (id == sg.getId()) {
					singleResultList = new ArrayList<StrategicGoal>();
					singleResultList.add(sg);
					break;
				}
			}
		} else {
			restClientUsage.getAll();
			singleResultList = new ArrayList<StrategicGoal>();
		}
		
		return singleResultList;
	}

	@Override
	public void onTaskComplete(List<StrategicGoal> result) {
		resultList = result;
		
		//do something with ws response
		dataReadyListener.dataReady();
	}

	@Override
	public void add(StrategicGoal element) {
		throw new NotImplementedException();
		
	}

	@Override
	public void delete(StrategicGoal element) {
		throw new NotImplementedException();
		
	}

}
